# coding=utf-8
'''
Created on 2018年8月4日

@author: Maxi
'''

import re
import sys
import calendar
import requests
project_name = "yaqh"
pro_name = "永安期货"
SELL = "持卖单量"
BUY = "持买单量"
dayfile = '20180802'
year = dayfile[0:4]
month = int(dayfile[4:6])
day = dayfile[6:8]
week = calendar.weekday(int(year),int(month),int(day))
#特殊处理月份和年份
month = int(dayfile[4:6]) - 1
if month == 0:
    year = int(year) - 1
    month = 12
print(week)
#如果是周末，那么直接退出
if week in [5,6]:
    print('周末不能生产交易数据，请重新输入日期')
    sys.exit(0)
# 需要采集的产品，字母是v
name = "聚氯乙烯"

#填充浏览器头部信息
data = {"memberDealPosiQuotes.variety": "v",
"memberDealPosiQuotes.trade_type": "0",
"year": year,
"month": month,
"day": day,
"contract.contract_id": "all",
"contract.variety_id": "v",
"currDate": dayfile}
request_url = "http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html"
headers = {
    'Content-Type':'application/x-www-form-urlencoded',
    'Host':'www.dce.com.cnm',
    'Origin':'http://www.dce.com.cn',
    'Referer':'http://www.dce.com.cn/publicweb/quotesdata/memberDealPosiQuotes.html',
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
}
response = requests.post(url=request_url,data=data,headers=headers)
html = response.text
filename = project_name + '.txt'

#获取html数据
with open(filename,'w')as f:
    f.write(html)
    f.close()
txt = open(project_name+ ".txt","r+")
filehtml = txt.read()
#替换多余字符
res_tr = r'<tr >(.*?)</tr>'
m_tr = re.findall(res_tr, filehtml, re.S | re.M)

#提取关键字数据
date = '日期:',dayfile
day_file = dayfile + '.txt'
fileopen = open(day_file, 'w')
fileopen.write(str(date))
for line in m_tr:
    print
    line
    res_td = r'<td >(.*?)</td>(.*?)<td  class="td-right">(.*?)</td>'
    m_td = re.findall(res_td, line, re.S | re.M)
    ya_str = str(m_td)
    ya_str = ya_str.replace("\\n\\n\\t\\t\\t\\t\\t\\t\\t\\t","")
    ya_str = ya_str.replace("')]","")
    ya_str = ya_str.replace("<td >","")
    ya_str = ya_str.replace("</td>","")
    result = ya_str.find(pro_name)
    if result >= 0:
        ya_list = ya_str.split("'",17)
        A_pro = ya_list[3]
        A = ya_list[5]
        B_pro = ya_list[9]
        B = ya_list[11]
        C_pro = ya_list[15]
        C = ya_list[17]
        result_txt = '\n',A_pro,' 成交量:',A,'\n',B_pro,' 持买单量:',B,'\n',C_pro,' 持卖单量:',C,'\n'
        #转换元祖
        result_txt = "".join(tuple(result_txt))
        fileopen.write(result_txt)

fileopen.close()
filehtml = open(day_file, 'r')
print('日期:',dayfile)
while 1:
    line = filehtml.readline()
    #匹配关键字商品
    if not line:
        break
    s = re.findall('永安期货\ 持(.*?)\:(\d+)*\,(\d+)*', line)
    if len(s):
        s = ''.join(str(s))
        s = s.replace("[('","")
        s = s.replace("', '","")
        s = s.replace("')]","")
        s = s.replace("量","量:")
        print(pro_name+'持'+s)